//
//  OJIssue.m
//  openJira
//
//  Created by Vincent Demay on 29/04/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJIssue.h"
#import "OJFilter.h"


@implementation OJIssue
@synthesize status, reporter, key, type, 
			updated, assigne, priority, summary, 
			vote, lid, description, project, 
			created;
@synthesize delegate;


- (void) createIssue {
	[[OJConnector getDefault] createIssue:self];
}

- (NSDictionary*) toNSDictionnary {
	NSMutableDictionary *toReturn = [[[NSMutableDictionary alloc] init] autorelease];
	[toReturn setObject:project forKey:@"project"];
	[toReturn setObject:description forKey:@"description"];
	[toReturn setObject:summary forKey:@"summary"];
	[toReturn setObject:[NSString stringWithFormat:@"%d", type.lid] forKey:@"type"];
	[toReturn setObject:assigne forKey:@"assignee"];
	return toReturn;
}

- (void) dealloc {
	[reporter release];
	//TODO : fixversion;
	[key release];
	[updated release];
	//TODO : components
	//TODO : affectsVersion
	[assigne release];
	[summary release];
	//TODO : customFieldValues
	[description release];
	[project release];
	[created release];
	[super dealloc];
}

////////////////////////// MANAGE RESPONSES ///////////////////
- (void) request:(XMLRPCRequest *)request onResponseSuccessfull:(XMLRPCResponse*) response{
	[delegate object:self isCreated:NO];
}

- (void) request:(XMLRPCRequest *)request onResponseError:(XMLRPCResponse*) response{
	NSLog([request body]);
	[delegate object:self isCreated:YES];
}

@end


///////////////////////// OJISSUELIST //////////////////////

@implementation OJIssueList
@synthesize issues, delegate;

- (id) init {
	self = [super init];
	if (self != nil) {
	}
	return self;
}


- (void) getIssuesFromFilter:(OJFilter*) filter{
	[[OJConnector getDefault] getIssues:self fromFilter:filter];
}


////////////////////////// MANAGE RESPONSES ///////////////////
- (void) request:(XMLRPCRequest *)request onResponseSuccessfull:(XMLRPCResponse*) response{
	id fromResponse = [response  object];
	[issues release];
	issues = [[NSMutableArray alloc] init];
	
	for (int i=0; i<[fromResponse count]; i++) {
		OJIssue *issue = [[OJIssue alloc] init];
		NSString *statusId = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"status"];
		issue.status = [[OJConnector getDefault].statusList.statuses objectForKey:statusId];
		
		issue.reporter = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"reporter"];
		
		issue.key = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"key"];
		
		NSString *typeId = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"type"];
		issue.type = [[OJConnector getDefault].typeList.types objectForKey:typeId];
		
		issue.updated = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"updated"];
		
		NSString *priorityId = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"priority"];
		issue.priority = [[OJConnector getDefault].priorityList.priorities objectForKey:priorityId];
		
		issue.assigne = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"assignee"];
		
		issue.summary = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"summary"];
		
		issue.vote = [[((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"votes"] intValue];
		
		issue.lid = [[((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"id"] intValue];
		
		issue.description = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"description"];
		
		issue.project = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"project"];
		
		issue.created = [((NSDictionary*)[fromResponse objectAtIndex:i]) objectForKey:@"created"];
		
		[issues addObject:issue];
		[issue release];
	}
	
	[delegate object:self isReady:NO];
}

- (void) request:(XMLRPCRequest *)request onResponseError:(XMLRPCResponse*) response{
	[delegate object:self isReady:YES];
}

@end
